/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.library.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.library.QuestionType;
import org.springblade.common.utils.SnowFlakeUtil;
import org.springblade.modules.library.bo.AutoExtractParamBO;
import org.springblade.modules.library.entity.QuestionBankEntity;
import org.springblade.modules.library.entity.QuestionOptionEntity;
import org.springblade.modules.library.vo.QuestionBankVO;
import org.springblade.modules.library.mapper.QuestionBankMapper;
import org.springblade.modules.library.service.IQuestionBankService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.library.service.IQuestionOptionService;
import org.springblade.modules.library.vo.QuestionTypeNumVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 试题题库 服务实现类
 *
 * @author
 * @since 2023-03-22
 */
@Service
@AllArgsConstructor
public class QuestionBankServiceImpl extends BaseServiceImpl<QuestionBankMapper, QuestionBankEntity> implements IQuestionBankService {

	private final IQuestionOptionService questionOptionService;

	@Override
	public IPage<QuestionBankEntity> selectQuestionBankPage(IPage<QuestionBankEntity> page, QuestionBankVO questionBank) {
		return page.setRecords(baseMapper.selectQuestionBankPage(page, questionBank));
	}

	@Override
	public IPage<QuestionBankVO> clientQuestionBankList(IPage<QuestionBankVO> page, QuestionBankVO questionBank) {
		return page.setRecords(baseMapper.clientQuestionBankList(page, questionBank));
	}

	@Override
	public List<QuestionBankVO> selectQuestionByParams(AutoExtractParamBO bo) {
		return baseMapper.selectQuestionByParams(bo);
	}


	@Override
	public List<QuestionBankVO> selectQuestionBankByParams(AutoExtractParamBO bo) {
		return baseMapper.selectQuestionBankByParams(bo);
	}

	@Override
	public QuestionBankVO selectQuestionDetails(QuestionBankEntity questionBank) {
		return baseMapper.selectQuestionDetails(questionBank);
	}

	@Override
	public List<QuestionBankVO> selectSubBankQuestionAndOption(Long subId,Integer type) {
		return baseMapper.selectSubBankQuestionAndOption(subId,type);
	}

	@Override
	public List<QuestionTypeNumVO> getQuestionTypeByBankNameAndQuestionProperties(QuestionBankEntity questionBankEntity) {
		return baseMapper.getQuestionTypeByBankNameAndQuestionProperties(questionBankEntity);
	}

	@Override
	public IPage<QuestionBankVO> getQuestionPage(IPage<QuestionBankVO> page, QuestionBankVO questionBank) {
		return page.setRecords(baseMapper.getQuestionPage(page, questionBank));
	}

	@Override
	public boolean questionCompleteFlag(Long questionId,Integer questionType) {

		//如果是简答题或者填空题，不做校验
		if (QuestionType.SHORT.getCode() ==questionType
			|| QuestionType.FILL.getCode() ==questionType ) return true;

		long count = questionOptionService.count(Wrappers.<QuestionOptionEntity>query().lambda().eq(QuestionOptionEntity::getQuestionBankId, questionId));

		//单选题、多选题 两个及两个以上的选项
		if (QuestionType.SINGLE.getCode() ==questionType
			|| QuestionType.MULTIPLE.getCode() ==questionType ){
			return count >= 2;
		}

		//判断题
		if (QuestionType.JUDGE.getCode() ==questionType){
			return count == 2;
		}
		return true;
	}


}
